#include "SideBarWindow.h"
#include "MainWindows.h"

namespace hps {
class MainWindow;

SideBarWindow::SideBarWindow(QWidget *parent, QHBoxLayout *layout)
    : m_parentLayout(layout) {
    m_sideBarlayout = new QVBoxLayout(this);

    m_more = new QPushButton();
    m_moreIcon = new QIcon("/home/hps/hps/Markdown-cpp/src/data/icon/more.svg");

    m_NoteBookAction = new QAction();
    m_dirAction = new QAction();
    m_searchAction = new QAction();

    m_noteBookButton = new hps::NoteBookButton();
    m_noteBookDirButton = new hps::NoteBookDirButton();
    m_searchButton = new hps::SearchButton();

    m_hideButtonAction = new QAction();
    m_moveMenu = new QMenu();
    m_moveTop = new QAction();
    m_moveBottom = new QAction();

    init();
    setSideBarUi();
    rspFunc();
}

void SideBarWindow::init() {
    this->setFixedWidth(40);
    m_sideBarlayout->setContentsMargins(5, 5, 5, 5);
    m_sideBarlayout->setAlignment(Qt::AlignTop);
    m_sideBarlayout->setSpacing(15);

    m_NoteBookAction->setText("笔记本");
    m_dirAction->setText("笔记目录");
    m_searchAction->setText("搜索");

    m_moreMenu.addAction(m_NoteBookAction);
//    m_moreMenu.addAction(m_dirAction);
//    m_moreMenu.addAction(m_searchAction);

    m_more->setIconSize(CONFIG()->getIconQSize());
    m_more->setObjectName("sideBarButton");
    m_more->setToolTip("更多工具窗口");

    m_noteBookButton->setToolTip("笔记本");
    m_noteBookDirButton->setToolTip("笔记目录");
    m_searchButton->setToolTip("搜索");

    m_moveTop->setText("顶部");
    m_moveBottom->setText("底部");
    m_hideButtonAction->setText("隐藏");

    m_moveMenu->setTitle("移至");

    m_rightClickButtonMenu.addAction(m_hideButtonAction);
    m_moveMenu->addAction(m_moveTop);
    m_moveMenu->addAction(m_moveBottom);
    m_rightClickButtonMenu.addMenu(m_moveMenu);
}

void SideBarWindow::setSideBarUi() {
    m_more->setIcon(*m_moreIcon);
    m_sideBarlayout->insertWidget(0, m_more);
}

void SideBarWindow::clickedMore() {
    if (m_moreMenu.actions().count() == 0) return;
    m_moreMenu.exec(m_more->mapToGlobal(QPoint(0, m_more->height())));
}

void SideBarWindow::rspFunc() {
    QObject::connect(m_more, &QPushButton::clicked, this, &SideBarWindow::clickedMore);
    QObject::connect(m_NoteBookAction, &QAction::triggered, this, &SideBarWindow::clickedNoteBookAction);
    QObject::connect(m_dirAction, &QAction::triggered, this, &SideBarWindow::clickedNoteBookDirAction);
    QObject::connect(m_searchAction, &QAction::triggered, this, &SideBarWindow::clickedSearchAction);
    QObject::connect(m_noteBookButton, &NoteBookButton::rightButtonClicked, this, &SideBarWindow::noteBookRightClicked);
    QObject::connect(m_noteBookDirButton,
                     &NoteBookDirButton::rightButtonClicked,
                     this,
                     &SideBarWindow::noteBookDirRightClicked);
    QObject::connect(m_searchButton, &SearchButton::rightButtonClicked, this, &SideBarWindow::searchRightClicked);
    QObject::connect(m_moveTop, &QAction::triggered, this, &SideBarWindow::moveButtonToTop);
    QObject::connect(m_moveBottom, &QAction::triggered, this, &SideBarWindow::moveButtonToBottom);
}

void SideBarWindow::clickedNoteBookAction() {
    m_moreMenu.removeAction(m_NoteBookAction);
    m_sideBarlayout->insertWidget(0, m_noteBookButton);
    m_buttonList.push_back(m_noteBookButton);
}

void SideBarWindow::clickedNoteBookDirAction() {
    m_moreMenu.removeAction(m_dirAction);
    m_sideBarlayout->insertWidget(0, m_noteBookDirButton);
    m_buttonList.push_back(m_noteBookDirButton);
}

void SideBarWindow::clickedSearchAction() {
    m_moreMenu.removeAction(m_searchAction);
    m_sideBarlayout->insertWidget(0, m_searchButton);
    m_buttonList.push_back(m_searchButton);
}
ButtonName::Button SideBarWindow::getNowOpenButtonName() {
    return m_openButtonName;
}
void SideBarWindow::setNowOpenButtonName(ButtonName::Button name) {
    m_openButtonName = name;
}
void SideBarWindow::setClickedColor(QPushButton *button) {
    button->setStyleSheet("background-color: #bfbfbf;");
}
void SideBarWindow::restoreColor(QPushButton *button) {
    button->setStyleSheet("");
}
void SideBarWindow::restoreAllColor() {
    for (auto button: m_buttonList) {
        button->setStyleSheet("");
    }
}

void SideBarWindow::removeButton(QWidget *widget, ButtonName::Button name) {
    m_sideBarlayout->removeWidget(widget);
    widget->setParent(nullptr);
    m_sideBarlayout->update();
    int s = m_buttonList.size();
    for (int i = 0; i < s; i ++) {
        if (m_buttonList[i] == widget) {
            m_buttonList.erase(m_buttonList.begin() + i);
            break;
        }
    }
    if (name == ButtonName::noteBook) {
        m_moreMenu.addAction(m_NoteBookAction);
    } else if (name == ButtonName::noteBookDir) {
        m_moreMenu.addAction(m_dirAction);
    } else if (name == ButtonName::search) {
        m_moreMenu.addAction(m_searchAction);
    }
}

void SideBarWindow::noteBookRightClicked() {
    showRightClickMenu(m_noteBookButton, ButtonName::noteBook);
}
void SideBarWindow::noteBookDirRightClicked() {
    showRightClickMenu(m_noteBookDirButton, ButtonName::noteBookDir);
    m_rightClickedButtonName = ButtonName::noteBookDir;
}
void SideBarWindow::searchRightClicked() {
    showRightClickMenu(m_searchButton, ButtonName::search);
}
void SideBarWindow::showRightClickMenu(QPushButton *pushButton, ButtonName::Button name) {
    m_rightClickedButtonName = name;
    m_rightClickButtonMenu.exec(pushButton->mapToGlobal(QPoint(0, pushButton->height())));
}
ButtonName::Button SideBarWindow::getRightClickedButtonName() {
    return m_rightClickedButtonName;
}
void SideBarWindow::setRightClickedButtonName(ButtonName::Button name) {
    m_rightClickedButtonName = name;
}
void SideBarWindow::moveButtonToTop() {
    if (ButtonName::noteBook == m_rightClickedButtonName) {
        m_sideBarlayout ->removeWidget(m_noteBookButton);
        m_sideBarlayout ->insertWidget(0, m_noteBookButton);
    } else if (ButtonName::noteBookDir == m_rightClickedButtonName) {
        m_sideBarlayout -> removeWidget(m_noteBookDirButton);
        m_sideBarlayout -> insertWidget(0, m_noteBookDirButton);
    } else if (ButtonName::search == m_rightClickedButtonName) {
        m_sideBarlayout -> removeWidget(m_searchButton);
        m_sideBarlayout -> insertWidget(0, m_searchButton);
    } else {
            std::cerr << "Move button error. In" << __FILE__ << ":" << __LINE__ << std::endl;
    }
    m_sideBarlayout -> update();
}
void SideBarWindow::moveButtonToBottom() {
    if (ButtonName::noteBook == m_rightClickedButtonName) {
        m_sideBarlayout -> removeWidget(m_noteBookButton);
        m_sideBarlayout -> insertWidget((int)(m_buttonList.size() - 1), m_noteBookButton);
    } else if (ButtonName::noteBookDir == m_rightClickedButtonName) {
        m_sideBarlayout -> removeWidget(m_noteBookDirButton);
        m_sideBarlayout -> insertWidget((int)(m_buttonList.size() - 1), m_noteBookDirButton);
    } else if (ButtonName::search == m_rightClickedButtonName) {
        m_sideBarlayout -> removeWidget(m_searchButton);
        m_sideBarlayout -> insertWidget((int)(m_buttonList.size() - 1), m_searchButton);
    } else {
        std::cerr << "Move button error. In" << __FILE__ << ":" << __LINE__ << std::endl;
    }
    m_sideBarlayout -> update();
}
}

